package cn.chiship.bs.api.biz.controller;

import cn.chiship.bs.api.biz.entity.*;
import cn.chiship.bs.api.biz.pojo.dto.ProductCategoryDto;
import cn.chiship.bs.api.biz.pojo.dto.ProductProductDto;
import cn.chiship.bs.api.biz.pojo.vo.CacheUserVO;
import cn.chiship.bs.api.biz.service.*;
import cn.chiship.sdk.core.annotation.Authorization;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.enums.UserTypeEnum;
import cn.chiship.sdk.core.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.math3.stat.descriptive.summary.Product;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品控制器
 *
 * @author lj
 */
@RestController
@Api(tags = "商品接口")
@RequestMapping("/product")
public class ProductProductController extends BaseController<ProductProduct, ProductProductExample> {
    @Resource
    ProductProductService productProductService;
    @Resource
    ProductTagsService productTagsService;
    @Resource
    UserCacheService userCacheService;

    @Override
    public BaseService<ProductProduct, ProductProductExample> getService() {
        return productProductService;
    }

    @ApiOperation(value = "商品分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", required = true, dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", required = true, dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序方式", defaultValue = "-gmtModified", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "productCategoryId", value = "所属分类", dataTypeClass = String.class, paramType = "query"),

    })
    @Authorization
    @GetMapping(value = "/page")
    public ResponseEntity<BaseResult> page(
            @RequestParam(required = false, value = "keyword") String keyword,
            @RequestParam(required = false, value = "productCategoryId") String productCategoryId) {
        ProductProductExample productProductExample = new ProductProductExample();
        ProductProductExample.Criteria criteria = productProductExample.createCriteria();

        if (!StringUtil.isNullOrEmpty(keyword)) {
            criteria.andProductNameLike(keyword + "%");
        }
        if (!StringUtil.isNullOrEmpty(productCategoryId)) {
            criteria.andProductCategoryIdEqualTo(productCategoryId);
        }
        return super.responseEntity(BaseResult.ok(super.page(productProductExample)));
    }

    @ApiOperation(value = "商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "所属分类", dataTypeClass = String.class, paramType = "query"),
    })

    @Authorization
    @GetMapping(value = "/list")
    public ResponseEntity<BaseResult> list(@RequestParam(required = false, value = "categoryId") String categoryId) {
        ProductProductExample productProductExample = new ProductProductExample();
        ProductProductExample.Criteria criteria = productProductExample.createCriteria();
        if (!StringUtil.isNullOrEmpty(categoryId)) {
            criteria.andProductCategoryIdEqualTo(categoryId);
        }
        CacheUserVO cacheUserVO = userCacheService.getUser();
        if (UserTypeEnum.MEMBER.getType().equals(cacheUserVO.getUserType())) {
            Object tag = cacheUserVO.getExtInfo();
            if (!StringUtil.isNull(tag)) {
                if (!StringUtil.isNullOrEmpty(tag.toString())) {
                    List<String> tagValue = StringUtil.strToListString(tag.toString(), ",");
                    ProductTagsExample productTagsExample = new ProductTagsExample();
                    productTagsExample.createCriteria().andNameIn(tagValue);
                    List<ProductTags> productTags = productTagsService.selectByExample(productTagsExample);
                    List<String> productIds = new ArrayList<>();
                    for (ProductTags productTag : productTags) {
                        productIds.add(productTag.getProductId());
                    }
                    if (productIds.isEmpty()) {
                        productIds.add("-1");
                    }
                    criteria.andIdIn(productIds);
                }
            }
        }
        productProductExample.setOrderByClause("orders asc");
        return super.responseEntity(BaseResult.ok(super.list(productProductExample)));
    }


    @ApiOperation(value = "保存商品")
    @PostMapping(value = "save")
    public ResponseEntity<BaseResult> save(@RequestBody @Valid ProductProductDto productProductDto) {
        ProductProduct product = new ProductProduct();
        BeanUtils.copyProperties(productProductDto, product);
        return super.responseEntity(super.save(product));
    }

    @ApiOperation(value = "更新商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "update/{id}")
    public ResponseEntity<BaseResult> update(@PathVariable("id") String id, @RequestBody @Valid ProductProductDto productProductDto) {
        ProductProduct product = new ProductProduct();
        BeanUtils.copyProperties(productProductDto, product);
        return super.responseEntity(super.update(id, product));
    }

    @ApiOperation(value = "删除商品数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键ID集合,多个ID,请用,分割", required = true, dataType = "List", paramType = "body")
    })
    @PostMapping(value = "remove")
    public ResponseEntity<BaseResult> remove(@RequestBody List<String> ids) {
        ProductProductExample productProductExample = new ProductProductExample();
        productProductExample.createCriteria().andIdIn(ids);
        return super.responseEntity(super.remove(productProductExample));

    }


}
